package week1;

import java.util.Scanner;

public class NumList {
    public static void main(String[] args) {

        Scanner Input = new Scanner(System.in);

        System.out.println("请输入一个整数： ");
        int num = Input.nextInt();

        NumNode head = new NumNode(num);

        String another = null;
        do
        {
            System.out.println("请再输入一个整数： ");
            int num2 = Input.nextInt();

            NumNode x1 = new NumNode(num2);
            add(head, x1);

            System.out.println("继续输入？ （y/n）");
            another = Input.nextLine();
            another = Input.nextLine();
        }
        while(another.equalsIgnoreCase("y"));

        NumNode x1 = new NumNode(666);
        NumNode x2 = new NumNode(240);
        NumNode x3 = new NumNode(212);
        NumNode x4 = new NumNode(123);

        System.out.println("添加结点“666”");
        add(head,x1);
        System.out.println("添加结点“240”");
        add(head,x2);
        System.out.println("添加结点“212”");
        add(head,x3);
        System.out.println("添加结点“666”");
        add(head,x4);

        PrintLinkedlist(head);
        System.out.println();

        System.out.println("删除结点“666”");
        System.out.println();
        delete(head, x1);

        PrintLinkedlist(head);

        selectionSort(head);
        System.out.println();
        System.out.println("排序后：");
        PrintLinkedlist(head);


    }






    private static class NumNode
    {
        protected NumNode next = null;
        protected int num;
        public NumNode(int num)
        {
            this.num = num;
            next = null;
        }
    }

    public static void PrintLinkedlist(NumNode Head) {
        NumNode node = Head;
        while (node != null) {
            System.out.print(" " + node.num);
            node = node.next;
        }
    }

    public static void add(NumNode head, NumNode node)
    {
        // 尾插法：在链表的尾部插入节点
        if ( head == null)
            head = node;
        else {
            NumNode temp = head;
            while (temp.next != null) {
                temp = temp.next;
            }
            temp.next = node;
        }
    }

    public static NumNode add2(NumNode head, NumNode node)
    {
        // 头插法：
        node.next = head;
        head = node;
        return head;
    }

    public static void add(NumNode Head, NumNode node1, NumNode node2) {
        NumNode point = Head;
        while ((point.num != node1.num) & point != null) {
            point = point.next;
        }
        if (point.num == node1.num) {
            node1.next = point.next;
            point.next = node2;
        }
    }

    public static void delete(NumNode head, NumNode node) {
        NumNode current = head, prev = head;

        while (current != null) {
            if (current.num != node.num) {
                prev = current;
                current = current.next;
            } else {
                break;
            }
        }
        prev.next = current.next;
        if (current.num != node.num)
            System.out.println("找不到节点，删除失败。");
    }

    public static void  selectionSort(NumNode head)
    {
        NumNode current = head;
        int temp;

        while (current != null)
        {
            NumNode numNode = current.next;
            while (numNode != null)
            {
                if (numNode.num < current.num)
                {
                    temp = current.num;
                    current.num = numNode.num;
                    numNode.num = temp;
                }
                numNode = numNode.next;
            }
            current = current.next;
        }
    }
}


